﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace STS.Account
{
    public partial class Register : System.Web.UI.Page
    {
        baza db = new baza();
        login_pass lp = new login_pass();
        dane_lekarzy dl = new dane_lekarzy();

        protected void Page_Load(object sender, EventArgs e)
        {
            
            Master.FindControl("NavigationMenu").Visible = false;
            Master.FindControl("NavigationMenuR").Visible = true;
            Label2.Visible = false;
            Button2.Visible = false;
            loginTextBox.Visible = false;
            Label3.Visible = false;
            Label4.Visible = false;
        }

        

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            dl.Imie = imieTextBox.Text;
            dl.Nazwisko = nazwiskoTextBox.Text;
            dl.typ_lekarza = Convert.ToInt32(DropDownList1.SelectedValue);
            dl.id_osrodka = 1;
            db.dane_lekarzies.InsertOnSubmit(dl);
            db.SubmitChanges();


            
            lp.p_l = 1;
            lp.id = dl.ID_Pracownika;
            if (checkLogin(imieTextBox.Text.Substring(0, 2).ToLower() + nazwiskoTextBox.Text.Substring(0, 3).ToLower() + DateTime.Now.Year.ToString().Substring(2, 2)))
            {
                lp.login = imieTextBox.Text.Substring(0, 2).ToLower() + nazwiskoTextBox.Text.Substring(0, 3).ToLower() + DateTime.Now.Year.ToString().Substring(2, 2);
                lp.password = Crypto.Encrypt("sts");
                db.login_passes.InsertOnSubmit(lp);
                db.SubmitChanges();
                Label2.Visible = true;
            }
            else
            {
                Label4.Visible = true;
                Button1.Visible = false;
                Button2.Visible = true;
                Label3.Visible = true;
                loginTextBox.Visible = true;
                
            }
            

        }

        public bool checkLogin(string login)
        {

            
            //Response.Write(" \n"+uid);
            //var ud = from u in db.dane_lekarzies where u.nr_uid == uid select u.nr_uid;
            var ud = db.login_passes.SingleOrDefault(fbu => fbu.login == login);
            //Response.Write(" \n" + ud);
            if (ud == null)
            {
                return true;
            }
            else return false;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            lp.p_l = 1;
            lp.id = dl.ID_Pracownika;
            lp.login = loginTextBox.Text;
            lp.password = Crypto.Encrypt("sts");
            db.login_passes.InsertOnSubmit(lp);
            db.SubmitChanges();
            Label2.Visible = true;
            
        }

        

        

    }
}
